<?php

namespace App\Http\Controllers;


use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\ConfigServices;
use App\Http\Services\NewYearGoodsServices;
use Illuminate\Http\Request;


//年货
class NewYearGoodsController extends Controller
{
    private $yearServices;
    private $configServices;


    public function __construct()
    {
        $this->yearServices = new NewYearGoodsServices();
        $this->configServices = new ConfigServices();
    }


    //自动开奖
    public function awardScoreList()
    {
        $this->checkDate();
        $this->yearServices->awardScoreList();
    }

    //房间开启年货礼包
    public function createNewYearGoods(Request $request)
    {
        $this->checkDate();
        $user_id = $request->input('user_id', 0);//创建用户
        $room_id = $request->input('room_id', 0);//创建房间
        $type = $request->input('type', 0);//开启类型 0 1 2
        $end_date = $request->input('end_date', 0);//结束时间 0 1 2
        $ratio = $request->input('ratio', 0);// 开奖比例 0 1 2
        $gift_id = $request->input('gift_id', 0);//放入礼物id
        if (intval($user_id) == 0 || intval($room_id) == 0) OutJson::toJson(-1, '失败');
        $res = $this->yearServices->createBag(intval($user_id), intval($room_id), $type, $end_date, $ratio, $gift_id);
        OutJson::toJson($res['code'], $res['msg'], $res['data']);
    }


    //用户点击
    public function partakeNewYearGoodsBag(Request $request)
    {
        $this->checkDate();
        $user_id = $request->input('user_id', 0);//参与用户
        $room_id = $request->input('room_id', 0);//参与房间
        if (intval($user_id) == 0 || intval($room_id) == 0) OutJson::toJson(-1, '失败');
        $res = $this->yearServices->partakeBag(intval($user_id), intval($room_id));
        OutJson::toJson($res['code'], $res['msg'], $res['data']);
    }


    //用户记录
    public function getUserLog(Request $request)
    {
        $user_id = $request->input('user_id', 0);
        $res = $this->yearServices->getUserLog($user_id);
        OutJson::toJson(0, '成功', $res);
    }


    //房间开启记录
    public function getRoomLog(Request $request)
    {
        $room_id = $request->input('room_id', 0);
        $res = $this->yearServices->getRoomLog($room_id);
        OutJson::toJson(0, '成功', $res);
    }

    //房间内礼包信息
    public function getRoomOpenInfo(Request $request)
    {
        $this->checkDate();
        $room_id = $request->input('room_id', 0);//参与房间
        $data = $this->yearServices->getOpenInfo($room_id);
        OutJson::toJson(0, '成功', $data);
    }

    //用户正在参与礼包信息
    public function getUserPartake(Request $request)
    {
        $user_id = $request->input('user_id', 0);//参与房间
        $data = $this->yearServices->getUserPartakeInfo($user_id);
        OutJson::toJson(0, '成功', $data);
    }

    //获取开启房间
    public function getOpenRoom(Request $request)
    {
        $data = $this->yearServices->getOpenBagRoom();
        OutJson::toJson(0, '成功', $data);
    }

//    public function getPond(Request $request)
//    {
//
//        $gift_id = $request->input('is_gift', 0);//参与房间
//        $type = $request->input('type', 0);//
//        $radio = $request->input('radio', 0);//
//        $diamond = $request->input('diamond', 0);//
//
//        $this->yearServices->getPond($gift_id, $type, $radio, $diamond);
//    }

    public function checkDate()
    {
        $check_date = $this->configServices->checkConfigDate(28, time(), 'new_year_date');
        if (!$check_date) {
            write_log('dishTimeOut', 'timeError', []);
            OutJson::toJson(3001, '活动过期', []);
        }
    }
}